package com.Day3;

public class Test {

    /*
    1、两个人结婚，能从其中一个人找到另一个人
    2、给其中一个人赋予Child属性，另一个人也能有(独生子)
     */

    public static void main(String[] args) {
        //新建一个丈夫对象nike
        Husband nike = new Husband();
        //给nike名字
        nike.name="nike";
        //新建一个孩子对象
        //nike.child=aaa Child();
        //给孩子名字mike
        //nike.child.name="mike";
        //新建一个妻子对象lily
        Wife lily = new Wife();
        //给lily名字
        lily.name="lily";
        //新建一个孩子对象
        lily.child=new Child();
        //给孩子名字nice
        lily.child.name="nice";
        //把nile,lily 关联起来：结婚
        married(nike,lily);
        System.out.println(nike.wife.name);
        //System.out.println(nike.wife.child.name);
        System.out.println(nike.child.name);
    }

    /**
     * 结婚
     * @param husbandname  丈夫名字
     * @param wifename  妻子名字
     */
    public static void married(Husband husbandname, Wife wifename){
        //给husbandname这个丈夫对象的Wife属性赋值：wifename
        husbandname.wife=wifename;
        //给wifename这个丈夫对象的Wife属性赋值：husbandname
        wifename.husband=husbandname;
        //如果丈夫有孩子，给妻子共享
        if (husbandname.child!=null){
            wifename.child=husbandname.child;
        }
        //如果妻子有孩子，给丈夫共享
        if (wifename.child!=null){
            husbandname.child=wifename.child;
        }
    }
}
